package com.my.metaData;

import com.my.user.util.DataSourceUtils;

import java.sql.*;

public class MetaDataDemo {
    private static final String URL="jdbc:mysql://localhost:3306/jsppro?generateSimpleParameterMetadata=true";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "12345678";

    public static void databaseMetaData(){
        try{
            Connection conn = DataSourceUtils.getDataSourceWithC3P0ByXML().getConnection();
            DatabaseMetaData dbMetaData = conn.getMetaData();
            String dbName = dbMetaData.getDatabaseProductName();//数据库名
            String dbVersion = dbMetaData.getDatabaseProductVersion();//数据库版本
            String driverName = dbMetaData.getDriverName();//驱动名
            String url = dbMetaData.getURL();//url
            String username = dbMetaData.getUserName();//userName
            ResultSet rs = dbMetaData.getPrimaryKeys(null,username,"student");
            //1.表类别     2.表模式       3.表名称
            //4.列名称（主键）5.主键中的序列号 6.主键的约束名称
            while(rs.next()){
                Object tableName = rs.getObject(3);
                Object columnName = rs.getObject(4);
                System.out.println(tableName + "---" + columnName);
                System.out.println(rs.getObject(1) + "---" + rs.getObject(2) + "---" + rs.getObject(5));
            }
            System.out.println(dbName);
            System.out.println(dbVersion);
            System.out.println(driverName);
            System.out.println(url);
            System.out.println(username);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void parameterMetaData(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
            PreparedStatement pstmt = conn.prepareStatement("select * from student where id = ? and name = ?");
            ParameterMetaData metaData = pstmt.getParameterMetaData();
            int count = metaData.getParameterCount();//占位符个数
            System.out.println(count);
            for(int i=1;i<=count;i++){
                //Mysql会出现Parameter metadata not available for the given statement异常
                //设置url为?generateSimpleParameterMetadata=true
                String typeName = metaData.getParameterTypeName(i);
                System.out.println(typeName);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void resultSetMetaData(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
            PreparedStatement pstmt = conn.prepareStatement("select * from student");
            ResultSet rs = pstmt.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            int count = metaData.getColumnCount();//列数
            System.out.println(count);
            //打印表列名
            for(int i=1;i<=count;i++){
                String columnName = metaData.getColumnName(i);
                System.out.print(columnName + "\t");
            }
            System.out.println("");
            //打印结果中的数据
            while(rs.next()){
                for(int i=1;i<=count;i++){
                    System.out.print(rs.getObject(i) + "\t");
                }
                System.out.println("");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    public static void main(String[] args) {
        resultSetMetaData();
    }
}
